package com.kk.xhr.admin.biz.impl;

import com.kk.xhr.admin.biz.IEmployeeBizService;
import com.kk.xhr.admin.controller.request.EducationExperienceAddRequest;
import com.kk.xhr.admin.controller.request.EducationExperienceModifyRequest;
import com.kk.xhr.admin.controller.request.EmployeeAddRequest;
import com.kk.xhr.admin.controller.request.EmployeeModifyRequest;
import com.kk.xhr.admin.controller.request.EmployeeQueryRequest;
import com.kk.xhr.admin.controller.request.JobExperienceAddRequest;
import com.kk.xhr.admin.controller.request.JobExperienceModifyRequest;
import com.kk.xhr.admin.controller.request.PrizeExperienceAddRequest;
import com.kk.xhr.admin.controller.request.PrizeExperienceModifyRequest;
import com.kk.xhr.admin.controller.request.ProjectExperienceAddRequest;
import com.kk.xhr.admin.controller.request.ProjectExperienceModifyRequest;
import com.kk.xhr.admin.controller.vo.EducationExperienceVO;
import com.kk.xhr.admin.controller.vo.EmployeeVO;
import com.kk.xhr.admin.controller.vo.JobExperienceVO;
import com.kk.xhr.admin.controller.vo.PrizeExperienceVO;
import com.kk.xhr.admin.controller.vo.ProjectExperienceVO;
import com.kk.xhr.admin.util.FileUtil;
import com.kk.xhr.common.ErrorCodeEnum;
import com.kk.xhr.common.exception.BusinessException;
import com.kk.xhr.common.pojo.DegreeEnum;
import com.kk.xhr.model.dto.EducationExperienceModifyRequestDTO;
import com.kk.xhr.model.dto.EmployeeDTO;
import com.kk.xhr.model.dto.EmployeeQueryRequestDTO;
import com.kk.xhr.model.dto.JobExperienceModifyRequestDTO;
import com.kk.xhr.model.dto.PageDto;
import com.kk.xhr.model.dto.PrizeExperienceModifyRequestDTO;
import com.kk.xhr.model.dto.ProjectExperienceModifyRequestDTO;
import com.kk.xhr.model.entity.EducationExperience;
import com.kk.xhr.model.entity.JobExperience;
import com.kk.xhr.model.entity.PrizeExperience;
import com.kk.xhr.model.entity.ProjectExperience;
import com.kk.xhr.model.service.IEmployeeService;
import com.kk.xhr.model.service.IUserService;
import com.kk.xhr.util.DateUtil;
import com.kk.xhr.web.vo.PageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * employee biz 接口实现
 *
 * @author luokexiong
 * @version 1.0 2021/2/15
 * @since 1.0.0
 */
@Service
@RequiredArgsConstructor
public class EmployeeBizServiceImpl implements IEmployeeBizService {
    private final IEmployeeService employeeService;
    private final IUserService userService;

    /**
     * {@inheritDoc}
     */
    @Override
    public Long add(Long userId, EmployeeAddRequest request) {
        return employeeService.add(generateDTO(userId, request));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long addPrize(PrizeExperienceAddRequest request) {
        return employeeService.addPrize(new PrizeExperience()
            .withEmployeeId(request.getEmployeeId())
            .withTitle(request.getTitle())
            .withFileId(request.getFileId())
            .withContent(request.getContent())
            .withAwardTime(DateUtil.validTime(request.getAwardTime())));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long addEducation(EducationExperienceAddRequest request) {
        return employeeService.addEducation(new EducationExperience()
            .withEmployeeId(request.getEmployeeId())
            .withSchool(request.getSchool())
            .withDegree(request.getDegree() == null ? null : request.getDegree().getValue())
            .withBegin(DateUtil.validTime(request.getBegin()))
            .withEnd(DateUtil.validTime(request.getEnd())));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long addProject(ProjectExperienceAddRequest request) {
        return employeeService.addProject(new ProjectExperience()
            .withEmployeeId(request.getEmployeeId())
            .withTitle(request.getTitle())
            .withRole(request.getRole())
            .withContent(request.getContent())
            .withBegin(DateUtil.validTime(request.getBegin()))
            .withEnd(DateUtil.validTime(request.getEnd())));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long addJob(JobExperienceAddRequest request) {
        return employeeService.addJob(new JobExperience()
            .withEmployeeId(request.getEmployeeId())
            .withCompany(request.getCompany())
            .withContent(request.getContent())
            .withPosition(request.getPosition())
            .withBegin(DateUtil.validTime(request.getBegin()))
            .withEnd(DateUtil.validTime(request.getEnd())));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public PageVO<EmployeeVO> queryPage(EmployeeQueryRequest request) {
        PageDto<EmployeeDTO> dtos = employeeService.query(
            EmployeeQueryRequestDTO.builder()
                .name(request.getName())
                .userId(request.getUserId())
                .status(request.getStatus())
                .limit(request.getLimit())
                .offset(request.getOffset())
                .build());
        if (0 == dtos.getTotal()) {
            return PageVO.empty();
        }
        return PageVO.fromDTO(dtos, this::generateVO);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public EmployeeVO queryEmployee(Long userId) {
        return generateVO(employeeService.queryByUserId(userId)
            .orElseThrow(() -> new BusinessException(ErrorCodeEnum.EMPLOYEE_NOT_EXIST)));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<JobExperienceVO> queryJob(Long employeeId) {
        return employeeService.queryJob(employeeId)
            .stream()
            .map(entity -> JobExperienceVO.builder()
                .id(entity.getId())
                .company(entity.getCompany())
                .employeeId(entity.getEmployeeId())
                .position(entity.getPosition())
                .content(entity.getContent())
                .begin(entity.getBegin())
                .end(entity.getEnd())
                .build())
            .collect(Collectors.toList());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<PrizeExperienceVO> queryPrize(Long employeeId) {
        return employeeService.queryPrize(employeeId)
            .stream()
            .map(entity -> PrizeExperienceVO.builder()
                .id(entity.getId())
                .employeeId(entity.getEmployeeId())
                .title(entity.getTitle())
                .content(entity.getContent())
                .fileUrl(FileUtil.getFileUrl(entity.getFileId()))
                .awardTime(entity.getAwardTime())
                .build())
            .collect(Collectors.toList());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<EducationExperienceVO> queryEducation(Long employeeId) {
        return employeeService.queryEducation(employeeId)
            .stream()
            .map(entity -> EducationExperienceVO.builder()
                .id(entity.getId())
                .employeeId(entity.getEmployeeId())
                .degree(DegreeEnum.from(entity.getDegree()))
                .school(entity.getSchool())
                .begin(entity.getBegin())
                .end(entity.getEnd())
                .build())
            .collect(Collectors.toList());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<ProjectExperienceVO> queryProject(Long employeeId) {
        return employeeService.queryProject(employeeId)
            .stream()
            .map(entity -> ProjectExperienceVO.builder()
                .id(entity.getId())
                .title(entity.getTitle())
                .content(entity.getContent())
                .role(entity.getRole())
                .begin(entity.getBegin())
                .end(entity.getEnd())
                .build())
            .collect(Collectors.toList());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modify(EmployeeModifyRequest request) {
        EmployeeDTO dto = EmployeeDTO.builder()
            .id(request.getId())
            .name(request.getName())
            .gender(request.getGender())
            .birthday(request.getBirthday())
            .idcard(request.getIdcard())
            .wedlock(request.getWedlock())
            .nationId(request.getNationId())
            .nativeplace(request.getNativeplace())
            .politicId(request.getPoliticId())
            .email(request.getEmail())
            .phone(request.getPhone())
            .address(request.getAddress())
            .form(request.getForm())
            .degree(request.getDegree())
            .specialty(request.getSpecialty())
            .school(request.getSchool())
            .begin(DateUtil.validTime(request.getBegin()))
            .end(DateUtil.validTime(request.getEnd()))
            .status(request.getStatus())
            .build();
        return employeeService.modify(dto);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modifyJob(JobExperienceModifyRequest request) {
        return employeeService.modifyJob(JobExperienceModifyRequestDTO.builder()
            .id(request.getId())
            .position(request.getPosition())
            .company(request.getCompany())
            .content(request.getContent())
            .begin(DateUtil.validTime(request.getBegin()))
            .end(DateUtil.validTime(request.getEnd()))
            .build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modifyPrize(PrizeExperienceModifyRequest request) {
        return employeeService.modifyPrize(PrizeExperienceModifyRequestDTO.builder()
            .id(request.getId())
            .title(request.getTitle())
            .content(request.getContent())
            .fileId(request.getFileId())
            .awardTime(DateUtil.validTime(request.getAwardTime()))
            .build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modifyEducation(EducationExperienceModifyRequest request) {
        return employeeService.modifyEducation(EducationExperienceModifyRequestDTO.builder()
            .id(request.getId())
            .school(request.getSchool())
            .degree(request.getDegree())
            .begin(DateUtil.validTime(request.getBegin()))
            .end(DateUtil.validTime(request.getEnd()))
            .build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modifyProject(ProjectExperienceModifyRequest request) {
        return employeeService.modifyProject(ProjectExperienceModifyRequestDTO.builder()
            .id(request.getId())
            .title(request.getTitle())
            .role(request.getRole())
            .content(request.getContent())
            .begin(DateUtil.validTime(request.getBegin()))
            .end(DateUtil.validTime(request.getEnd()))
            .build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean removeJob(Long id) {
        return employeeService.removeJob(id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean removePrize(Long id) {
        return employeeService.removePrize(id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean removeEducation(Long id) {
        return employeeService.removeEducation(id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean removeProject(Long id) {
        return employeeService.removeProject(id);
    }

    /**
     * 生成vo
     *
     * @param dto EmployeeDTO
     * @return EmployeeVO
     */
    private EmployeeVO generateVO(EmployeeDTO dto) {
        return EmployeeVO.builder()
            .id(dto.getId())
            .avatarUrl(dto.getAvatarUrl())
            .workId(userService.queryWorkId(dto.getUserId()))
            .name(dto.getName())
            .gender(dto.getGender())
            .birthday(dto.getBirthday())
            .idcard(dto.getIdcard())
            .wedlock(dto.getWedlock())
            .nationId(dto.getNationId())
            .nativeplace(dto.getNativeplace())
            .politicId(dto.getPoliticId())
            .email(dto.getEmail())
            .phone(dto.getPhone())
            .address(dto.getAddress())
            .form(dto.getForm())
            .degree(dto.getDegree())
            .specialty(dto.getSpecialty())
            .school(dto.getSchool())
            .begin(dto.getBegin())
            .end(dto.getEnd())
            .status(dto.getStatus())
            .build();
    }

    /**
     * 生成dto
     *
     * @param userId  用户id
     * @param request EmployeeAddRequest
     * @return EmployeeDTO
     */
    private EmployeeDTO generateDTO(Long userId, EmployeeAddRequest request) {
        return EmployeeDTO.builder()
            .name(request.getName())
            .userId(userId)
            .gender(request.getGender())
            .birthday(request.getBirthday())
            .idcard(request.getIdcard())
            .wedlock(request.getWedlock())
            .nationId(request.getNationId())
            .nativeplace(request.getNativeplace())
            .politicId(request.getPoliticId())
            .email(request.getEmail())
            .phone(request.getPhone())
            .address(request.getAddress())
            .form(request.getForm())
            .degree(request.getDegree())
            .specialty(request.getSpecialty())
            .school(request.getSchool())
            .avatarUrl(null)
            .begin(DateUtil.validTime(request.getBegin()))
            .end(DateUtil.validTime(request.getEnd()))
            .status(request.getStatus())
            .build();
    }
}
